/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package meuteste;

import java.io.IOException;

/**
 *
 * @author tiago
 */
public class TelaExibirRotas1 extends javax.swing.JFrame {

    private PortaSerial portaSerial;
    private TelaSemRotaGerente telaSemRotaGerente;
    private TelaExibirRotas2 telaExibirRotas2;
    private TelaErroNoFormatoDoProtocolo telaErroNoFormatoDoProtocolo;
    private TelaCoordenadorOcupado telaCoordenadorOcupado;

    /**
     * Creates new form TelaRotas
     */
    public TelaExibirRotas1(PortaSerial portaSerial, TelaSemRotaGerente telaSemRotaGerente, TelaErroNoFormatoDoProtocolo telaErroNoFormatoDoProtocolo, TelaCoordenadorOcupado telaCoordenadorOcupado) {
        initComponents();
        this.portaSerial = portaSerial;
        telaExibirRotas2 = new TelaExibirRotas2();
        this.telaSemRotaGerente = telaSemRotaGerente;
        this.telaErroNoFormatoDoProtocolo = telaErroNoFormatoDoProtocolo;
        this.telaCoordenadorOcupado = telaCoordenadorOcupado;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabelExibirRotas = new javax.swing.JLabel();
        jTextFieldExibirRotas = new javax.swing.JTextField();
        jButtonExibirRotas1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Tela Rotas");

        jLabelExibirRotas.setText("Digite o no que deseja exibir rota");

        jButtonExibirRotas1.setText("Ok!");
        jButtonExibirRotas1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jButtonExibirRotas1MouseReleased(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabelExibirRotas, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jTextFieldExibirRotas)
                    .addComponent(jButtonExibirRotas1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(12, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabelExibirRotas)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jTextFieldExibirRotas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButtonExibirRotas1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonExibirRotas1MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonExibirRotas1MouseReleased
        this.setVisible(false);
        String resposta;
        boolean recebiResposta = false;
        while (!recebiResposta) {
            try {
                resposta = portaSerial.requisitarRotas(new StringBuilder(this.jTextFieldExibirRotas.getText())); //enviarRTCUnicast(new StringBuilder(this.textFieldTelaRTCUnicast1.getText()));
                switch (resposta) {
                    case "iR?t":
                        telaSemRotaGerente.setVisible(true);
                        recebiResposta = true;
                        break;
                    case "iP?t":
                        telaErroNoFormatoDoProtocolo.setVisible(true);
                        recebiResposta = true;
                        break;
                    case "":
                        System.out.println("Não veio resposta");
                        break;
                    case "iiiiT?t":
                        telaCoordenadorOcupado.setVisible(true);
                        recebiResposta = true;
                        break;
                    default:
                        if (!resposta.matches("iX.*"))  {
                            StringBuilder stb = new StringBuilder(resposta);
                            StringBuilder stbJLabelExibirRotas2 = new StringBuilder(1000);
                            int indexPontoVirgula = 0;
                            indexPontoVirgula = stb.indexOf(";", indexPontoVirgula + 1);
                            if (indexPontoVirgula == -1) {
                                stbJLabelExibirRotas2.append(stb.substring(1, stb.length() - 1));
                            } else {
                                boolean ultimaRota = false;
                                int indexInicio = 1;
                                while (!ultimaRota) {
                                    if (indexPontoVirgula == -1) {
                                        ultimaRota = !ultimaRota;
                                        stbJLabelExibirRotas2.append(stb.substring(indexInicio, stb.length() - 1));
                                    } else {
                                        stbJLabelExibirRotas2.append(stb.substring(indexInicio, indexPontoVirgula)).append("\n");
                                        indexInicio = indexPontoVirgula + 1;
                                        indexPontoVirgula = stb.indexOf(";", indexPontoVirgula + 1);
                                    }
                                }
                            }
                            telaExibirRotas2.getJLabeltelaExibirRotas2().setText(stbJLabelExibirRotas2.substring(0));
                            telaExibirRotas2.pack();
                            telaExibirRotas2.setVisible(true);
                            recebiResposta = true;
                        } else {
                            System.out.println("=== Aguardando ===");
                            Thread.sleep(35000);
                        }
                        break;
                }
            } catch (IOException | InterruptedException ex) {
            }
            jTextFieldExibirRotas.setText("");
        }
    }//GEN-LAST:event_jButtonExibirRotas1MouseReleased

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(TelaExibirRotas1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TelaExibirRotas1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TelaExibirRotas1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TelaExibirRotas1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                //      new TelaExibirRotas1().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonExibirRotas1;
    private javax.swing.JLabel jLabelExibirRotas;
    private javax.swing.JTextField jTextFieldExibirRotas;
    // End of variables declaration//GEN-END:variables
}
